package com.gxuwz.medical.domain.medical;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.exception.DbException;

/**
 * 医疗机构管理类
 * 
 * @author 演示
 * 
 */
public class Medical {

	/**
	 * 机构编码
	 */
	private String jgbm;
	/**
	 * 组织机构编码
	 */
	private String zzjgbm;
	/**
	 * 机构名称
	 */
	private String jgmc;
	/**
	 * 地区编码
	 */
	private String dqbm;
	/**
	 * 行政区域编码
	 */
	private String areacode;
	/**
	 * 隶属关系
	 */
	private S201 lsgx;

	/**
	 * 机构级别
	 */
	private S201 jgjb;
	/**
	 * 申报定点类型
	 */
	private S201 sbddlx;
	/**
	 * 批准定点类型
	 */
	private S201 pzddlx;
	/**
	 * 所属经济类型
	 */
	private S201 ssjjlx;
	/**
	 * 卫生机构大类
	 */
	private S201 wsjgdl;
	/**
	 * 卫生机构小类
	 */
	private S201 wsjgxl;

	/**
	 * 主管单位
	 */
	private String zgdw;
	/**
	 * 开业时间
	 */
	private String kysj;
	/**
	 * 法人代表
	 */
	private String frdb;
	/**
	 * 注册资金
	 */
	private double zczj;

	public Medical() {

	}

	/**
	 * 全参构造函数
	 * 
	 * @param jgbm
	 * @param zzjgbm
	 * @param jgmc
	 * @param dqbm
	 * @param areacode
	 * @param lsgx
	 * @param jgjb
	 * @param sbddlx
	 * @param pzddlx
	 * @param ssjjlx
	 * @param wsjgdl
	 * @param wsjgxl
	 * @param zgdw
	 * @param kysj
	 * @param frdb
	 * @param zczj
	 */
	public Medical(String jgbm, String zzjgbm, String jgmc, String dqbm,
			String areacode, S201 lsgx, S201 jgjb, S201 sbddlx, S201 pzddlx,
			S201 ssjjlx, S201 wsjgdl, S201 wsjgxl, String zgdw, String kysj,
			String frdb, double zczj) {

		this.jgbm = jgbm;
		this.zzjgbm = zzjgbm;
		this.jgmc = jgmc;
		this.dqbm = dqbm;
		this.areacode = areacode;
		this.lsgx = lsgx;
		this.jgjb = jgjb;
		this.sbddlx = sbddlx;
		this.pzddlx = pzddlx;
		this.ssjjlx = ssjjlx;
		this.wsjgdl = wsjgdl;
		this.wsjgxl = wsjgxl;
		this.zgdw = zgdw;
		this.kysj = kysj;
		this.frdb = frdb;
		this.zczj = zczj;
	}

	/**
	 * 加载农合机构信息
	 * 
	 * @param jgbm
	 * @throws Exception
	 */
	public Medical(String id) throws Exception {
		this.jgbm = id;
		try {
			loadDB();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DbException("无法找到ID=" + this.jgbm + "对应机构信息");
		}
	}

	private void loadDB() throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_medical where jgbm=?");
			int index = 1;
			pstmt.setString(index, this.jgbm);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.jgbm = rs.getString("jgbm");
				this.zzjgbm = rs.getString("zzjgbm");
				this.jgmc = rs.getString("jgmc");
				this.dqbm = rs.getString("dqbm");
				this.areacode = rs.getString("areacode");
				this.lsgx = new S201(rs.getString("lsgx"), "02");
				this.jgjb = new S201(rs.getString("jgjb"), "06");
				this.sbddlx = new S201(rs.getString("sbddlx"), "04");
				this.pzddlx = new S201(rs.getString("pzddlx"), "04");
				this.ssjjlx = new S201(rs.getString("ssjjlx"), "01");
				this.wsjgdl = new S201(rs.getString("wsjgdl"), "03");
				this.wsjgxl = new S201(rs.getString("wsjgxl"), "0301");
				this.zgdw = rs.getString("zgdw");
				this.kysj = rs.getString("kysj");
				this.frdb = rs.getString("frdb");
				this.zczj = rs.getDouble("zczj");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

	/**
	 * 添加医疗机构
	 * 
	 * @throws SQLException
	 */
	public void add() throws SQLException {
		Connection conn = null;
		try {
			// 1:获得连接
			conn = DbUtil.getConn();
			// 2;保存到数据库
			conn.setAutoCommit(false);
			saveToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_medical( jgbm, zzjgbm, jgmc, dqbm, areacode, lsgx, jgjb, "
					+ "sbddlx, pzddlx, ssjjlx, wsjgdl, wsjgxl, zgdw, kysj , frdb,zczj)");
			sqlBuff.append("values");
			sqlBuff.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?,?)");
			String sql = sqlBuff.toString();
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数的值
			int index = 1;
			pstmt.setString(index++, this.jgbm);
			pstmt.setString(index++, this.zzjgbm);
			pstmt.setString(index++, this.jgmc);
			pstmt.setString(index++, this.dqbm);
			pstmt.setString(index++, this.areacode);
			pstmt.setString(index++, this.lsgx.getItemcode());
			pstmt.setString(index++, this.jgjb.getItemcode());
			pstmt.setString(index++, this.sbddlx.getItemcode());
			pstmt.setString(index++, this.pzddlx.getItemcode());
			pstmt.setString(index++, this.ssjjlx.getItemcode());
			pstmt.setString(index++, this.wsjgdl.getItemcode());
			pstmt.setString(index++, this.wsjgxl.getItemcode());
			pstmt.setString(index++, this.zgdw);
			pstmt.setString(index++, this.kysj);
			pstmt.setString(index++, this.frdb);
			pstmt.setDouble(index++, this.zczj);

			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}

	}

	/**
	 * 自己写的删除和修改的信息
	 * 
	 * @param areaname
	 * @throws Exception
	 */

	public void editMe() throws Exception {
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			editToDB(conn);
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	public void delMe(String jgbm) throws Exception {
		Connection conn = null;
		try {
			this.jgbm = jgbm;
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			delFromDB(conn);
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void editToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update t_medical "
							+ "set zzjgbm=?,jgmc=?,dqbm=?,areacode=?,lsgx=?,jgjb=?,sbddlx=?,pzddlx=?,ssjjlx=?,"
							+ "wsjgdl=?,wsjgxl=?,zgdw=?,kysj=?,frdb=?,zczj=? where jgbm=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.zzjgbm);
			pstmt.setString(2, this.jgmc);
			pstmt.setString(3, this.dqbm);
			pstmt.setString(4, this.areacode);
			pstmt.setString(5, this.lsgx.getItemcode());
			pstmt.setString(6, this.jgjb.getItemcode());
			pstmt.setString(7, this.sbddlx.getItemcode());
			pstmt.setString(8, this.pzddlx.getItemcode());
			pstmt.setString(9, this.ssjjlx.getItemcode());
			pstmt.setString(10, this.wsjgdl.getItemcode());
			pstmt.setString(11, this.wsjgxl.getItemcode());
			pstmt.setString(12, this.zgdw);
			pstmt.setString(13, this.kysj);
			pstmt.setString(14, this.frdb);
			pstmt.setDouble(15, this.zczj);
			pstmt.setString(16, this.jgbm);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

	private void delFromDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"delete from t_medical where jgbm=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.jgbm);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

	// 结束线--------------------------------------------

	public String getJgbm() {
		return jgbm;
	}

	public void setJgbm(String jgbm) {
		this.jgbm = jgbm;
	}

	public String getZzjgbm() {
		return zzjgbm;
	}

	public void setZzjgbm(String zzjgbm) {
		this.zzjgbm = zzjgbm;
	}

	public String getJgmc() {
		return jgmc;
	}

	public void setJgmc(String jgmc) {
		this.jgmc = jgmc;
	}

	public String getDqbm() {
		return dqbm;
	}

	public void setDqbm(String dqbm) {
		this.dqbm = dqbm;
	}

	public String getAreacode() {
		return areacode;
	}

	public S201 getLsgx() {
		return lsgx;
	}

	public void setLsgx(S201 lsgx) {
		this.lsgx = lsgx;
	}

	public S201 getJgjb() {
		return jgjb;
	}

	public void setJgjb(S201 jgjb) {
		this.jgjb = jgjb;
	}

	public S201 getSbddlx() {
		return sbddlx;
	}

	public void setSbddlx(S201 sbddlx) {
		this.sbddlx = sbddlx;
	}

	public S201 getPzddlx() {
		return pzddlx;
	}

	public void setPzddlx(S201 pzddlx) {
		this.pzddlx = pzddlx;
	}

	public S201 getSsjjlx() {
		return ssjjlx;
	}

	public void setSsjjlx(S201 ssjjlx) {
		this.ssjjlx = ssjjlx;
	}

	public S201 getWsjgdl() {
		return wsjgdl;
	}

	public void setWsjgdl(S201 wsjgdl) {
		this.wsjgdl = wsjgdl;
	}

	public S201 getWsjgxl() {
		return wsjgxl;
	}

	public void setWsjgxl(S201 wsjgxl) {
		this.wsjgxl = wsjgxl;
	}

	public String getZgdw() {
		return zgdw;
	}

	public void setZgdw(String zgdw) {
		this.zgdw = zgdw;
	}

	public String getFrdb() {
		return frdb;
	}

	public void setFrdb(String frdb) {
		this.frdb = frdb;
	}

	public double getZczj() {
		return zczj;
	}

	public void setZczj(double zczj) {
		this.zczj = zczj;
	}

	public void setAreacode(String areacode) {
		this.areacode = areacode;
	}

	public String getKysj() {
		return kysj;
	}

	public void setKysj(String kysj) {
		this.kysj = kysj;
	}

}
